package planit.view;
import java.awt.event.ActionEvent;

import java.util.ArrayList;
import javax.swing.AbstractAction;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import planit.controller.ControllerFactory;
import planit.controller.ControllerFactory.ControllerType;
import planit.model.Group;
import planit.model.Model;
import planit.model.Person;
import planit.view.wrappers.GroupWrapper;
import planit.view.wrappers.ModelCellRenderer;
import planit.view.wrappers.PersonWrapper;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class AddPersonToGroupDialog extends javax.swing.JDialog {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel selectGroupLabel;
	private JScrollPane jScrollPane2;
	private AbstractAction CancelAction;
	private JButton removeFromGroupButton;
	private JScrollPane jScrollPane3;
	private JButton cancelButton;
	private JButton addPerson;
	private JList groupPersonsList;
	private JLabel descriptionLabel;
	private JLabel groupPersonsLabel;
	private JTextArea descriptionTextArea;
	private JTextField nameTextField;
	private JLabel nameLabel;
	private JList personList;
	private JScrollPane jScrollPane1;
	private JLabel selectPersonLabel;
	private JList groupList;
	private Model model;

	public AddPersonToGroupDialog(Model model) {
		super();
		this.model = model;
		initGUI();
		updateGroupList();
		updatePersonList();
		//groupList.setSelectedIndex(0);
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				{
					selectGroupLabel = new JLabel();
					getContentPane().add(selectGroupLabel);
					selectGroupLabel.setText("Select Group");
					selectGroupLabel.setBounds(12, 12, 119, 16);
				}
				{
					jScrollPane3 = new JScrollPane();
					getContentPane().add(jScrollPane3);
					jScrollPane3.setBounds(12, 34, 102, 209);
					{
						ListModel groupListModel = new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
						groupList = new JList();
						jScrollPane3.setViewportView(groupList);
						groupList.setModel(groupListModel);
						groupList.setBounds(12, 34, 102, 209);
						groupList
								.addListSelectionListener(new ListSelectionListener() {
									public void valueChanged(
											ListSelectionEvent evt) {
										groupListValueChanged(evt);
									}
								});
					}
				}
				{
					selectPersonLabel = new JLabel();
					getContentPane().add(selectPersonLabel);
					selectPersonLabel.setText("Select Person");
					selectPersonLabel.setBounds(12, 249, 93, 16);
				}
				{
					jScrollPane2 = new JScrollPane();
					getContentPane().add(jScrollPane2);
					jScrollPane2.setBounds(12, 265, 102, 185);
					{
						ListModel personListModel = new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
						personList = new JList();
						jScrollPane2.setViewportView(personList);
						personList.setModel(personListModel);
						personList.setBounds(12, 265, 102, 185);
					}
				}
				{
					nameLabel = new JLabel();
					getContentPane().add(nameLabel);
					nameLabel.setText("Group name:");
					nameLabel.setBounds(126, 34, 79, 16);
				}
				{
					nameTextField = new JTextField();
					getContentPane().add(nameTextField);
					nameTextField.setBounds(126, 56, 246, 23);
				}
				{
					descriptionLabel = new JLabel();
					getContentPane().add(descriptionLabel);
					descriptionLabel.setText("Description:");
					descriptionLabel.setBounds(126, 85, 73, 16);
				}
				{
					descriptionTextArea = new JTextArea();
					getContentPane().add(descriptionTextArea);
					descriptionTextArea.setBounds(126, 101, 246, 58);
				}
				{
					groupPersonsLabel = new JLabel();
					getContentPane().add(groupPersonsLabel);
					groupPersonsLabel.setText("Persons in group:");
					groupPersonsLabel.setBounds(126, 165, 107, 16);
				}
				{
					addPerson = new JButton();
					getContentPane().add(addPerson);
					addPerson.setAction(ControllerFactory
							.getController(ControllerType.ADD_PERSON_TO_GROUP));
					addPerson.getAction().putValue("AddPersonToGroupDialog",
							this);
					addPerson.setText("Add to Group");
					addPerson.setBounds(12, 456, 102, 28);
				}
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton);
					getContentPane().add(getJScrollPane1());
					getContentPane().add(getRemoveFromGroupButton());
					cancelButton.setText("Close");
					cancelButton.setBounds(311, 456, 61, 28);
					cancelButton.setAction(getCancelAction());
				}
			}
			this.setSize(394, 528);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void groupListValueChanged(ListSelectionEvent evt) {
		if (groupList.getSelectedValue() == null) {
			nameTextField.setText("");
			descriptionTextArea.setText("");
			groupPersonsList.removeAll();
		} else {
			Group temp;
			temp = model.getSchedule().getGroup(
					((GroupWrapper) groupList.getSelectedValue()).getId());

			nameTextField.setText(temp.getName());
			descriptionTextArea.setText(temp.getDescription());
			updateGroupPersonsList();

		}
	}

	public void updateGroupList() {
		Group[] group = new Group[0];
		ArrayList<JLabel> allGroups = new ArrayList<JLabel>();
		group = model.getSchedule().getGroups().toArray(group);
		for (int i = 1; i < group.length; i++) {
			allGroups.add(new GroupWrapper(group[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allGroups.toArray());
		groupList.setModel(list2Model);
		groupList.setCellRenderer(new ModelCellRenderer());
	}

	public void updateGroupPersonsList() {
		Group temp;
		temp = model.getSchedule().getGroup(
				((GroupWrapper) groupList.getSelectedValue()).getId());

		Person[] person = new Person[0];
		ArrayList<JLabel> allPersons = new ArrayList<JLabel>();
		person = temp.getPersons().toArray(person);
		for (int i = 0; i < person.length; i++) {
			allPersons.add(new PersonWrapper(person[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allPersons.toArray());
		groupPersonsList.setModel(list2Model);
		groupPersonsList.setCellRenderer(new ModelCellRenderer());
	}

	public void updatePersonList() {
		Person[] person = new Person[0];
		ArrayList<JLabel> allPersons = new ArrayList<JLabel>();
		person = model.getSchedule().getPersons().toArray(person);
		for (int i = 0; i < person.length; i++) {
			allPersons.add(new PersonWrapper(person[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allPersons.toArray());
		personList.setModel(list2Model);
		personList.setCellRenderer(new ModelCellRenderer());
	}

	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(126, 181, 240, 269);
			{
				ListModel groupPersonsListModel = new DefaultComboBoxModel(
						new String[] { "Item One", "Item Two" });
				groupPersonsList = new JList();
				jScrollPane1.setViewportView(groupPersonsList);
				groupPersonsList.setModel(groupPersonsListModel);
				groupPersonsList.setBounds(126, 181, 240, 269);
				groupPersonsList.setPreferredSize(new java.awt.Dimension(232,
						248));
			}
		}
		return jScrollPane1;
	}

	public int getGroupId() {
		return ((GroupWrapper) groupList.getSelectedValue()).getId();
	}

	public int getPersonId() {
		return ((PersonWrapper) personList.getSelectedValue()).getId();
	}
	public int getPersonInGroupId(){
		return ((PersonWrapper) groupPersonsList.getSelectedValue()).getId();
	}
	
	private JButton getRemoveFromGroupButton() {
		if(removeFromGroupButton == null) {
			removeFromGroupButton = new JButton();
			removeFromGroupButton.setAction(ControllerFactory.getController(ControllerType.REMOVE_PERSON_FROM_GROUP));
			removeFromGroupButton.getAction().putValue("RemovePersonFromGroup" +
					"Dialog", this);
			removeFromGroupButton.setText("Remove from group");
			removeFromGroupButton.setBounds(120, 456, 137, 28);
		}
		return removeFromGroupButton;
	}
	
	private AbstractAction getCancelAction() {
		if(CancelAction == null) {
			CancelAction = new AbstractAction("Close", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return CancelAction;
	}
}
